/*
 * Votante.cpp
 *
 *  Created on: Oct 8, 2011
 *      Author: lore
 */

#include "Votante.h"

Votante::Votante() {
}

void Votante::setDni(unsigned long dnivot){
	dni=dnivot;
}
unsigned long Votante::getDni(){
	return dni;

}
void Votante::setNombreApellido(string nomyapevot){
	nomyape=nomyapevot;

}

string Votante::getNombreApellido(){
	return nomyape;

}

void Votante::setClaveEncriptada(string clavevot){
	clave=clavevot;
}
string Votante:: getClaveEncriptada (){
	return clave;
}
void Votante::setDomicilio(string domiciliovot){
	domicilio=domiciliovot;
}
string Votante::getDomicilio() {
	return domicilio;
}

void Votante::setDistritoId(unsigned long distritovot) {
	distritoId=distritovot;

}
unsigned long Votante::getDistritoId(){
	return distritoId;

}

Votante::~Votante() {
}


void Votante::removeAllElecciones() {
	idElecciones.clear();
}
void Votante::addEleccion(unsigned long idEleccion) {
	idElecciones.push_back(idEleccion);
}
vector<unsigned long> Votante::getElecciones() {
	return idElecciones;
}
void Votante::setEleccionesId(vector<unsigned long> ids) {
	removeAllElecciones();
	for (unsigned long i = 0; i < ids.size(); i++) {
		idElecciones.push_back(ids.at(i));
	}
}

char* Votante::pack() {
	char* buffer = new char[packLen()];

	unsigned long size = 0;

	char*ptr = reinterpret_cast<char*>(&dni);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i] = *ptr;
		ptr++;
	}
	size = sizeof(unsigned long);

	ptr = reinterpret_cast<char*>(&distritoId);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);

	unsigned long tam = clave.size();

//	cout << "guardando clave: " << clave << " len(" << tam << ")" << endl;

	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);

	const char*str0 = clave.c_str();
	for (unsigned long i = 0; i < clave.size(); i++) {
		buffer[i+size] = str0[i];
	}
	size += clave.size();
	/*
	ptr = reinterpret_cast<char*>(&clave);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);
*/

	tam = nomyape.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);

	const char*str = nomyape.c_str();
	for (unsigned long i = 0; i < nomyape.size(); i++) {
		buffer[i+size] = str[i];
	}
	size += nomyape.size();

	tam = domicilio.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);

	const char*str2 = domicilio.c_str();
	for (unsigned long i = 0; i < domicilio.size(); i++) {
		buffer[i+size] = str2[i];
	}
	size += domicilio.size();

	tam = idElecciones.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+size] = *ptr;
		ptr++;
	}
	size += sizeof(unsigned long);

	for (unsigned long j = 0; j < idElecciones.size(); j++) {
		unsigned long idEleccion = idElecciones.at(j);
		ptr = reinterpret_cast<char*>(&idEleccion);
		for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
			buffer[i+size] = *ptr;
			ptr++;
		}
		size += sizeof(unsigned long);
	}
	
	if (size != packLen()) {
		cerr << "Erro al empaquetar el Votante" << endl;
	}

	return buffer;
}

unsigned long Votante::packLen() {
	unsigned long len = 0;

	len += 3*sizeof(unsigned long); // dni, clave, distrito
	len += 3*sizeof(unsigned long); // tamaño elecciones, nombre y domicilio
	len += idElecciones.size()*sizeof(unsigned long);
	len += nomyape.size();
	len += domicilio.size();
	len += clave.size();

	return len;
}

bool Votante::unpack(char* buffer, unsigned long bufferLen) {
	unsigned long size = 0;

	char*ptr = reinterpret_cast<char*>(&dni);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	ptr = reinterpret_cast<char*>(&distritoId);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	/*
	ptr = reinterpret_cast<char*>(&clave);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);
	*/
// clave
	unsigned long tam = 0;
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	stringstream ss0;
	for (unsigned long i = 0; i < tam; i++) {
		ss0 << buffer[i+size];
	}
	clave = ss0.str();
	size += clave.size();

// nombre
	tam = 0;
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	stringstream ss;
	for (unsigned long i = 0; i < tam; i++) {
		ss << buffer[i+size];
	}
	nomyape = ss.str();
	size += nomyape.size();
	
	/// domiciolio
	tam = 0;
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	stringstream ss2;
	for (unsigned long i = 0; i < tam; i++) {
		ss2 << buffer[i+size];
	}
	domicilio = ss2.str();
	size += domicilio.size();
	
	tam = 0;
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+size];
		ptr++;
	}
	size += sizeof(unsigned long);

	idElecciones.clear();
	for (unsigned long j = 0; j < tam ; j++) {
		unsigned long idEleccion = 0;
		ptr = reinterpret_cast<char*>(&idEleccion);
		for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
			*ptr = buffer[i+size];
			ptr++;
		}
		idElecciones.push_back(idEleccion);
		size += sizeof(unsigned long);
	}

	if (size == bufferLen) {
		return true;
	}

	return false;
}

